package com.hqyj.product.controller;

import com.hqyj.product.model.GoodInfo;
import frame.product.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/goodController")
public class GoodController extends BaseController {
    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, GoodInfo goodInfo) {
        //1.接收数据;
        System.out.println("-----------list-------");
        //3.调用业务;

        List<GoodInfo> goodInfo_dbList = goodInfoService.queryAllGoodInfo();
        System.out.println(goodInfo_dbList);
        model.addAttribute("goodInfo_dbList", goodInfo_dbList);
        //2.跳转页面;
        return "view/product/goodInfo/goodInfoList";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, GoodInfo goodInfo) {
        //1.接收数据;
//        System.out.println("-----------add------" + goodInfo);
        //3.调用业务;
        int n = goodInfoService.addGoodInfoByGoodInfo(goodInfo);

        //2.跳转页面;
        List<GoodInfo> goodInfo_dbList = goodInfoService.queryAllGoodInfo();
        model.addAttribute("goodInfo_dbList", goodInfo_dbList);
        return "/view/product/goodInfo/goodInfoList";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, GoodInfo goodInfo, String idStr) {
        //1.接收数据;
        System.out.println("-----------delete------" + idStr);
        //3.调用业务;
        //字符类型转数字类型
        int id = Integer.parseInt(idStr);
        //业务
        int n = goodInfoService.deleteGoodInfoById(id);

        //2.跳转页面;
        List<GoodInfo> goodInfo_dbList = goodInfoService.queryAllGoodInfo();
        model.addAttribute("goodInfo_dbList", goodInfo_dbList);
        return "view/product/goodInfo/goodInfoList";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, GoodInfo goodInfo, String idStr) {
        //字符类型转数字类型
        int goodInfoId = Integer.parseInt(idStr);
        //业务
        GoodInfo goodInfo_db = goodInfoService.queryGoodInfoById(goodInfoId);
        System.out.println("-----------updateUI------" + goodInfo_db);
        model.addAttribute("goodInfo", goodInfo_db);
        //2.跳转页面;
        return "view/product/goodInfo/updateGoodInfo";
    }

    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, GoodInfo goodInfo, String id) {
        //1.接收数据;
        //字符类型转数字类型
        int pickingListNo = Integer.parseInt(id);


        //3.调用业务;
        int n = goodInfoService.updateGoodInfoByGoodInfo(goodInfo);


        //2.跳转页面;
        List<GoodInfo> goodInfo_dbList = goodInfoService.queryAllGoodInfo();
        model.addAttribute("goodInfo_dbList", goodInfo_dbList);
        return "view/product/goodInfo/goodInfoList";
    }

    @RequestMapping("/send.do")
    public String send(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, GoodInfo goodInfo, String idStr) {
        //1.接收数据;
        GoodInfo goodInfo_db = goodInfoService.queryGoodInfoById(Integer.parseInt(idStr));
        goodInfoService.updateGoodInfoStateByGoodInfo(Integer.parseInt(idStr));
        String type = "原料";

        //3.调用业务;
        int m = goodInfoService.insertOutRepertoryByGoodInfo(goodInfo_db.getTradeName(),goodInfo_db.getPickingQuantity(),type);

        //2.跳转页面;
        List<GoodInfo> goodInfo_dbList = goodInfoService.queryAllGoodInfo();
        model.addAttribute("goodInfo_dbList", goodInfo_dbList);
        return "view/product/goodInfo/goodInfoList";
    }
}
